//
//  CustomCollectionCell.m
//  CollectionTest
//
//  Created by A on 2020/11/7.
//  Copyright © 2020 TyTeam. All rights reserved.
//

#import "CustomCollectionCell.h"
#import "UIView+Extent.h"
#import "LoadAndSaveImage.h"

@interface CustomCollectionCell ()
@property (nonatomic, strong) UIImageView *mainImg;
@property (nonatomic, strong) UIImageView *headimg;
@property (nonatomic, strong) UILabel *mainLab;
@property (nonatomic, strong) UILabel *secondLab;



@property (nonatomic, strong) UIView *backView;

@end

@implementation CustomCollectionCell

- (void)buildCellWithData:(RDMTCellData *)data {
    
    [[LoadAndSaveImage shareManager] removeAllCashe];
    
    if (self.backView) {
        [self.backView removeFromSuperview];
        self.backView = nil;

        [self.mainImg removeFromSuperview];
        self.mainImg = nil;

        [self.headimg removeFromSuperview];
        self.headimg = nil;

        [self.mainLab removeFromSuperview];
        self.mainLab = nil;

        [self.secondLab removeFromSuperview];
        self.secondLab = nil;
    }
    
    self.backView = [[UIView alloc] initWithFrame:self.bounds];
    self.backView.backgroundColor = [UIColor grayColor];
    [self addSubview:self.backView];
    
    
    NSUInteger type = data.DataType;
    

    switch (type) {
        case 1:
        {
            if (data.Image) {
                ImageData *imgData = data.Image;
                UIImage *image = [[LoadAndSaveImage shareManager] loadImageWithUrlString:imgData.ImageUrl];
                self.mainImg = [[UIImageView alloc] init];
                self.mainImg.frame = CGRectMake(0, 0, self.width, self.width * (imgData.ImageHeight/imgData.ImageWidth));
                self.mainImg.image = image;
                [self.backView addSubview:self.mainImg];
                
            }
            
            if (![data.HeadImgBase64 isEqualToString:@""] && data.HeadImgBase64 != nil) {
                UIImage *headImg = [UIImage imageWithData:[[NSData alloc] initWithBase64EncodedString:data.HeadImgBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]];
                self.headimg = [[UIImageView alloc] init];
                self.headimg.image = headImg;
                
                self.headimg.frame = CGRectMake(10, self.mainImg.bottom+10, 40, 40);
   
                [self.backView addSubview:self.headimg];
            }
            
        }
            break;
        case 2:
        {
            if (![data.TitleText isEqualToString:@""]) {
                self.mainLab = [[UILabel alloc] init];
                self.mainLab.textColor = [UIColor redColor];
                self.mainLab.numberOfLines = 0;
                self.mainLab.frame = CGRectMake(0, 10, self.width, 20);
                self.mainLab.text = data.TitleText;
                self.mainLab.font = [UIFont systemFontOfSize:15];
                [self.mainLab sizeToFit];
                [self.backView addSubview:self.mainLab];

            }
            
            if (![data.SecondTitleText isEqualToString:@""]) {
                self.secondLab = [[UILabel alloc] init];
                self.secondLab.textColor = [UIColor blueColor];
                self.secondLab.numberOfLines = 0;
                self.secondLab.frame = CGRectMake(0, self.mainLab.bottom+10, self.width, 20);
                self.secondLab.text = data.SecondTitleText;
                self.secondLab.font = [UIFont systemFontOfSize:15];
                [self.secondLab sizeToFit];
                [self.backView addSubview:self.secondLab];
            }
        }
            break;
        case 3:
        {
            BOOL haveMainImg = NO;
            
            if (data.Image) {
                ImageData *imgData = data.Image;
                UIImage *image = [[LoadAndSaveImage shareManager] loadImageWithUrlString:imgData.ImageUrl];
                self.mainImg = [[UIImageView alloc] init];
                self.mainImg.frame = CGRectMake(0, 0, self.width, self.width * (imgData.ImageHeight/imgData.ImageWidth));
                self.mainImg.image = image;
                [self.backView addSubview:self.mainImg];
                haveMainImg = YES;
            }
            
            if (![data.TitleText isEqualToString:@""]) {
                self.mainLab = [[UILabel alloc] init];
                self.mainLab.textColor = [UIColor redColor];
                self.mainLab.numberOfLines = 0;
                
                if (haveMainImg) {
                    self.mainLab.frame = CGRectMake(0, self.mainImg.bottom+10, self.width, 20);
                }else {
                    self.mainLab.frame = CGRectMake(0, 10, self.width, 20);
                }
                
                self.mainLab.text = data.TitleText;
                self.mainLab.font = [UIFont systemFontOfSize:15];
                [self.mainLab sizeToFit];
                [self.backView addSubview:self.mainLab];

            }
            
            if (![data.SecondTitleText isEqualToString:@""]) {
                self.secondLab = [[UILabel alloc] init];
                self.secondLab.textColor = [UIColor blueColor];
                self.secondLab.numberOfLines = 0;
                self.secondLab.frame = CGRectMake(0, self.mainLab.bottom+10, self.width, 20);
                self.secondLab.text = data.SecondTitleText;
                self.secondLab.font = [UIFont systemFontOfSize:15];
                [self.secondLab sizeToFit];
                [self.backView addSubview:self.secondLab];
            }
            
            
            if (![data.HeadImgBase64 isEqualToString:@""] && data.HeadImgBase64 != nil) {
                UIImage *headImg = [UIImage imageWithData:[[NSData alloc] initWithBase64EncodedString:data.HeadImgBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]];
                self.headimg = [[UIImageView alloc] init];
                self.headimg.frame = CGRectMake(10, self.secondLab.bottom+10, 40, 40);
                self.headimg.image = headImg;
                [self.backView addSubview:self.headimg];
            }
        }
            break;
            
        default:
            break;
    }
    
 
}
@end
